<template>
  <menuComp :menu="menu"></menuComp>
</template>
<script setup lang="ts">
import menuComp from '../components/menuComp.vue'
import { useHistory } from '@renderer/utils/useHistory'
import useDelete from '@renderer/utils/useDelete'
import { saveModel } from '@renderer/api'
import { ElMessage } from 'element-plus'
import { storeToRefs } from 'pinia'
import { useViewportStore } from '@renderer/stores/viewport'
import emiter from '@renderer/utils/mitt'

function handleRevocation() {
  handleUndo(undoStackList.value.length - 2)
}

function handleRecover() {
  handleRedo(0)
}

const { undoStackList, handleUndo, handleRedo, commitHistory } = useHistory()
const { deleteObject } = useDelete()
function handleDelete() {
  deleteObject()
}

// 清空
const viewportStore = useViewportStore()
const { publicGroup, notPublicGroup } = storeToRefs(viewportStore)
async function handleClear() {
  await saveModel([])
  publicGroup.value?.clear()
  notPublicGroup.value?.clear()
  emiter.emit('viewChange')
  commitHistory('清空', 'clear')
  ElMessage.success('已清空')
}

const menu = {
  label: '编辑',
  children: [
    { label: '撤销', shortcutKey: 'Ctrl+Z', click: handleRevocation },
    { label: '恢复', shortcutKey: 'Ctrl+Y', click: handleRecover },
    { label: '删除', shortcutKey: 'Ctrl+Del', click: handleDelete },
    { label: '清空', shortcutKey: '', click: handleClear }
  ]
}
</script>
<style lang="scss" scoped></style>
